package com.noble.glamour.ui.agora.manager;

import com.noble.glamour.module.local.chat.msgtype.bean.BaseMessage;
import com.noble.glamour.module.local.chat.msgtype.type.InviteVideoMessage;
import com.noble.glamour.module.local.chat.msgtype.type.VideoMessage;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;

import java.util.HashMap;
import java.util.Map;

/**
 * 音视频通信消息管理类： 统一管理，预防丢消息
 * <p>
 * Created by Su on 2017/8/2.
 */
public class RtcMessageMgr {
    private volatile static RtcMessageMgr mInstance;

    private RtcMessageMgr() {
    }

    public static RtcMessageMgr getInstance() {
        if (mInstance == null)
            synchronized (RtcMessageMgr.class) {
                if (mInstance == null)
                    mInstance = new RtcMessageMgr();
            }
        return mInstance;
    }

    public void release() {
        mMessage = null;
    }

    // ============================= 维护音视频通信消息 =========================
    private BaseMessage mMessage = new BaseMessage();    // 维护最新通话消息信息

    /**
     * 判断是否是无效通话
     *
     * @return true 通话无效
     */
    public boolean leaveChat() {
        // 本次通话已失效, 或已收到本次通话的挂断消息
        boolean status = false;

        if (mMessage instanceof VideoMessage) {
            status = ((VideoMessage) mMessage).getVideoTp() == 3 ||
                    ((VideoMessage) mMessage).getVideoTp() == 4;
        }

        if (status) {
            ModuleMgr.getRtcEnginMgr().getEngineConfig().reason = "收到挂断消息，直接关闭";
        }

        return !checkVcId() || status;
    }

    /**
     * 悬浮窗：判断是否已接通
     *
     * @return true 已收到同意消息
     */
    public boolean inChat() {
        boolean status = false;

        if (mMessage instanceof VideoMessage)
            status = ((VideoMessage) mMessage).getVideoTp() == 2;

        return status;
    }


    private boolean checkVcId() {
        if (mMessage == null)
            return true;

        long vcId = ModuleMgr.getRtcEnginMgr().getEngineConfig().mVcid;
        if (mMessage instanceof VideoMessage) {
            return ((VideoMessage) mMessage).getVideoID() == 0 || vcId == ((VideoMessage) mMessage).getVideoID();
        }

        if (mMessage instanceof InviteVideoMessage) {
            return vcId == ((InviteVideoMessage) mMessage).getInvite_id();
        }

        return true;
    }

    /**
     * 保存通话消息
     */
    public void addVideoMsg(BaseMessage msg) {
        this.mMessage = msg;
    }


    // ===================== 分发音视频消息 =====================

    /**
     * 群发有人接受消息
     */
    public void sendGroupAcceptMsg(VideoMessage videoMessage) {
        ModuleMgr.getRtcEnginMgr().getEngineConfig().mOtherId = videoMessage.getLWhisperID();
        ModuleMgr.getRtcEnginMgr().getEngineConfig().mVcid = videoMessage.getVideoID();
        ModuleMgr.getRtcEnginMgr().getEngineConfig().mChannel = String.valueOf(videoMessage.getVideoID());
        ModuleMgr.getRtcEnginMgr().getEngineConfig().mChatType = videoMessage.getVideoMediaTp();
        MsgMgr.getInstance().sendMsg(MsgType.MT_Rtc_Group_Chat_Msg, null);
    }

    /**
     * @param vcTp 请求类型，1邀请加入聊天，2同意加入  3拒绝或取消 4挂断（挂断可能会收到不止一次）
     */
    public void sendVideoMsg(int vcTp, String vcChannelKey) {
        Map<String, Object> data = new HashMap<>();
        data.put("type", vcTp);
        ModuleMgr.getRtcEnginMgr().getEngineConfig().mChannelKey = vcChannelKey;
        MsgMgr.getInstance().sendMsg(MsgType.MT_Rtc_Chat_Status, data);
    }
}
